﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Com.Crc.ResManage.BLL.EquipManage;
using Com.Crc.PoJo.Model.UserBoard;
using Com.Crc.PoJo.Model.Resource;
using Com.App_Code;

namespace Com.Crc.ResManage.Web.EquipManage.PON
{
    public partial class PONManageSubUserBoardSub : BasePage
    {
        PONManageUserBoardBLL bll;

        public string OLTID
        {
            get
            {
                if (ViewState["OLTID"] != null)
                {
                    return ViewState["OLTID"].ToString();
                }

                return "";
            }
            set
            {
                ViewState["OLTID"] = value;
            }
        }

        public string UserBoardID
        {
            get
            {
                if (ViewState["UserBoardID"] != null)
                {
                    return ViewState["UserBoardID"].ToString();
                }

                return "";
            }
            set
            {
                ViewState["UserBoardID"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitControl();
            }
        }

        private void InitControl()
        {   
            if (Request.QueryString["oltid"] != null)
            {
                OLTID = Request.QueryString["oltid"].ToString();
            }

            if (Request.QueryString["userboardid"] != null)
            {
                UserBoardID = Request.QueryString["userboardid"].ToString();
            }

            InitControlManagerBLL bllManager = new InitControlManagerBLL();
            DataTable dtUserBoardType = bllManager.QueryCommonDicInfo(Dictionary.COMMON_DIC_TYPE.RESOURCE_OLT_PONUSERBOARD_TYPE);
            this.rblUserBoradType.DataSource = dtUserBoardType;
            this.rblUserBoradType.DataTextField = "s_name";
            this.rblUserBoradType.DataValueField = "n_id";
            this.rblUserBoradType.DataBind();

            if (Request.QueryString["optype"] != null && Request.QueryString["optype"].ToString() == "0")
            {
                LoadUserBoardData();
            }

            this.ddlSlot.Items.AddRange(new ListItem[]
            {
                new ListItem("1","1"),
                new ListItem("2","2"),
                new ListItem("3","3"),
                new ListItem("4","4"),
                new ListItem("5","5"),
                new ListItem("6","6"),
                new ListItem("7","7"),
                new ListItem("8","8"),
                new ListItem("11","11"),
                new ListItem("12","12"),
                new ListItem("13","13"),
                new ListItem("14","14"),
                new ListItem("15","15"),
                new ListItem("16","16"),
                new ListItem("17","17"),
                new ListItem("18","18")
            });
            bll = new PONManageUserBoardBLL();
            DataTable dt = bll.QueryOLTUserBoardData("", "", this.OLTID, "");

            foreach(DataRow row in dt.Rows)
            {
                if (Request.QueryString["optype"] != null && Request.QueryString["optype"].ToString() == "0")
                {
                    if (row["N_ID"].ToString() == UserBoardID)
                    {
                        this.ddlSlot.Items.FindByValue(row["N_SLOT_NO"].ToString()).Selected = true;
                        continue;
                    }
                }
                
                ddlSlot.Items.Remove(new ListItem(row["N_SLOT_NO"].ToString(),row["N_SLOT_NO"].ToString()));
            }
            

        }

        private void LoadUserBoardData()
        {
            bll = new PONManageUserBoardBLL();
            DataTable dt = bll.QueryOLTUserBoardData("", "", "", UserBoardID);

            InitControlHelp.SetSelectedValue(this.ddlSlot, dt.Rows[0]["N_SLOT_NO"].ToString());
            this.txtBeginVlan.Text = dt.Rows[0]["N_QNQVLAN_S"].ToString();
            this.txtEndVlan.Text = dt.Rows[0]["N_QNQVLAN_E"].ToString();
            this.rblUserBoradType.SelectedIndex = this.rblUserBoradType.Items.IndexOf(
                this.rblUserBoradType.Items.FindByValue(dt.Rows[0]["S_TYPE"].ToString()));
        }

        protected void grd_InitializeLayout(object sender, Infragistics.WebUI.UltraWebGrid.LayoutEventArgs e)
        {
            grd.Bands[0].Columns.Clear();

            int i = 0;
            base.AddGridCheckColumn(this.grd, i++, grd.ClientID);
            base.AddGridColumn(this.grd, "N_ID", "N_ID", 80, i++, true);
            base.AddGridColumn(this.grd, "用户板数量", "N_CAP", 90, i++, false);
            base.AddGridColumn(this.grd, "用户板类型", "N_TYPE", 100, i++, false);
            base.AddGridColumn(this.grd, "起始端口号", "N_PORT_S", 80, i++, false);
            base.AddGridColumn(this.grd, "结束端口号", "N_PORT_E", 80, i++, false);
            base.AddGridColumn(this.grd, "起始槽位号", "N_TREN_S", 80, i++, false);
            base.AddGridColumn(this.grd, "结束槽位号", "N_TREN_E", 80, i++, false);
            base.AddGridColumn(this.grd, "起始Vlan", "N_VLAN_S", 80, i++, false);
            base.AddGridColumn(this.grd, "结束Vlan", "N_VLAN_E", 80, i++, false);

            base.AddGridImageColumn(grd, "编辑", "Edit", 40, i++, "~/img/icon_edit.gif");

            this.grd.Columns.FromKey("N_CAP").Width = Unit.Percentage(100);
        }

        protected void grd_PageIndexChanged(object sender, Infragistics.WebUI.UltraWebGrid.PageEventArgs e)
        {

        }

        protected void btnDeleteONU_Click(object sender, EventArgs e)
        {

        }

        protected void btnAddUserBoard_Click(object sender, EventArgs e)
        {
            bll = new PONManageUserBoardBLL();

            PONUserBoardEntity ponUserBoardData = new PONUserBoardEntity();
            ponUserBoardData.N_OLT_ID = long.Parse(OLTID);
            ponUserBoardData.N_SLOT_NO = long.Parse(ddlSlot.Items[this.ddlSlot.SelectedIndex].Value);
            ponUserBoardData.N_QNQVLAN_E = long.Parse(txtBeginVlan.Text.Trim());
            ponUserBoardData.N_QNQVLAN_S = long.Parse(txtEndVlan.Text.Trim());
            ponUserBoardData.S_TYPE = this.rblUserBoradType.Items[this.rblUserBoradType.SelectedIndex].Value;

            if (Request.QueryString["optype"] != null && Request.QueryString["optype"].ToString() == "0")
            {
                //修改
                ponUserBoardData.N_ID = long.Parse(this.UserBoardID);
                bll.UpdateOLTUserBoardData(ponUserBoardData);
                base.ShowAlertForUpdatePanel("用户板修改成功！");
            }
            else
            {
                //新增
                bll.AddOLTUserBoardData(ponUserBoardData);
                base.ShowAlertForUpdatePanel("用户板新增成功！");
            }
        }

        protected void btnClearUserBoard_Click(object sender, EventArgs e)
        {
            this.txtBeginVlan.Text = "";
            this.txtEndVlan.Text = "";
        }

        protected void btn_SearchONU_Click(object sender, EventArgs e)
        {

        }
    }
}